package com.baizhi.controller;

import com.baizhi.annotation.LogAnnotation;
import com.baizhi.common.Constant;
import com.baizhi.entity.CmfzAudio;
import com.baizhi.service.CmfzAudioService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("cmfzAudio")
@Slf4j
public class CmfzAudioController {
    @Autowired
    private CmfzAudioService cmfzAudioService;

    @RequestMapping("show")
    @LogAnnotation(content = "查询一个专辑下的所有音频",type = "查询")
    public Map showCmfzAudioByAlbumId(Integer albumId,Integer page,Integer limit){
        Map map = new HashMap();
        Page<CmfzAudio> cmfzAudioPage = cmfzAudioService.selectByAlbumId(albumId, page, limit);
        map.put("code",0);
        map.put("msg","");
        map.put("count",cmfzAudioPage.getTotal());
        map.put("data",cmfzAudioPage.getRecords());
        return map;
    }

    @RequestMapping("upload")
    @LogAnnotation(content = "上传音频图片",type = "上传")
    public Map upload(MultipartFile file){
        Map map = new HashMap();
        String filename = file.getOriginalFilename();
        String newFileName = UUID.randomUUID().toString()+filename;
        try {
            file.transferTo(new File(Constant.ALBUM_IMG_LOCATION,newFileName));
            map.put("flag",true);
            map.put("fileName",newFileName);
            log.info("上传音频图片成功后的新名字："+newFileName);
        } catch (IOException e) {
            map.put("flag",false);
        }
        return map;
    }

    @RequestMapping("add")
    @LogAnnotation(content = "添加音频信息",type = "添加")
    public Map addCmfzAudio(CmfzAudio cmfzAudio){
        Map map = new HashMap();
        try {
            cmfzAudioService.insertCmfzAudio(cmfzAudio);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }

    @RequestMapping("remove")
    @LogAnnotation(content = "删除音频信息",type = "删除")
    public Map removeCmfzAudio(Integer audioId){
        Map map = new HashMap();
        try {
            cmfzAudioService.deleteCmfzAudio(audioId);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }

    @RequestMapping("getOne")
    @LogAnnotation(content = "查询一个音频的信息",type = "查询")
    public CmfzAudio getOneArticle(Integer audioId){
        CmfzAudio cmfzAudio = cmfzAudioService.selectById(audioId);
        log.info("要修改的专辑信息："+cmfzAudio);
        return cmfzAudio;
    }

    @RequestMapping("update")
    @LogAnnotation(content = "更新音频信息",type = "修改")
    public Map updateCmfzAudio(CmfzAudio cmfzAudio){
        log.info("修改专辑方法的参数："+cmfzAudio);
        Map map = new HashMap();
        try {
            cmfzAudioService.updateCmfzAudio(cmfzAudio);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
}
